package main

import (
	"fmt"
	"sync"
	"time"
)

/*
银行账户:
存取时必须同步(不能并发操作)
其余操作可以并发
*/

type Account struct {
	money int
	mt    sync.Mutex
}

/*存钱*/
func (a *Account) Save(amount int) int {
	//抢麦
	a.mt.Lock()
	fmt.Println("正在存钱...")
	<-time.After(10 * time.Second)
	a.money += amount
	fmt.Println("存钱结束")
	a.mt.Unlock()
	return a.money
}

/*取钱*/
func (a *Account) GetMoney(amount int) int {
	//抢麦
	a.mt.Lock()
	fmt.Println("正在取钱...")
	<-time.After(10 * time.Second)
	a.money -= amount
	fmt.Println("取钱结束")
	a.mt.Unlock()
	return a.money
}

/*打印上个月的银行流水*/
func (a *Account) PrintHistory() {
	fmt.Println("打印上个月的银行流水")
}

/*查询余额*/
func (a *Account) Query() int {
	fmt.Println("当前余额", a.money)
	return a.money
}

/*数据初始化*/
var waitGroup sync.WaitGroup
var account *Account
//var account2 *Account
//var account3 *Account
func init() {
	account = new(Account)
	account.money = 10000
}

func main() {

	waitGroup.Add(1)
	go func() {
		account.Save(500)
		waitGroup.Done()
	}()

	waitGroup.Add(1)
	go func() {
		account.GetMoney(500)
		waitGroup.Done()
	}()

	waitGroup.Add(1)
	go func() {
		account.PrintHistory()
		waitGroup.Done()
	}()

	waitGroup.Add(1)
	go func() {
		account.Query()
		waitGroup.Done()
	}()

	waitGroup.Wait()
	fmt.Println("END")
}
